package cn.edu.ncut.cs.springboot.petmanagementsystem.controller;

import cn.edu.ncut.cs.springboot.petmanagementsystem.common.Result;
import cn.edu.ncut.cs.springboot.petmanagementsystem.pojo.SearchHistory;
import cn.edu.ncut.cs.springboot.petmanagementsystem.service.SearchService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 搜索控制器
 */
@Tag(name = "搜索模块")
@RestController
@RequestMapping("/search")
@RequiredArgsConstructor
public class SearchController {
    
    private final SearchService searchService;
    
    /**
     * 全局搜索
     */
    @Operation(summary = "全局搜索")
    @GetMapping
    public Result<Map<String, Object>> globalSearch(
            @Parameter(description = "搜索关键词") @RequestParam String keyword,
            @Parameter(description = "搜索类型(all-全部, product-商品, service-服务)") @RequestParam(defaultValue = "all") String type,
            Authentication authentication) {
        
        Long userId = null;
        if (authentication != null && authentication.isAuthenticated()) {
            userId = Long.parseLong(authentication.getName());
        }
        
        Map<String, Object> result = searchService.globalSearch(keyword, type, userId);
        return Result.success(result);
    }
    
    /**
     * 搜索建议
     */
    @Operation(summary = "获取搜索建议")
    @GetMapping("/suggest")
    public Result<List<String>> getSearchSuggestions(
            @Parameter(description = "关键词前缀") @RequestParam String keyword) {
        
        List<String> suggestions = searchService.getSearchSuggestions(keyword);
        return Result.success(suggestions);
    }
    
    /**
     * 热门搜索
     */
    @Operation(summary = "获取热门搜索")
    @GetMapping("/hot")
    public Result<List<String>> getHotSearches(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") int limit) {
        
        List<String> hotSearches = searchService.getHotSearches(limit);
        return Result.success(hotSearches);
    }
    
    /**
     * 搜索历史
     */
    @Operation(summary = "获取用户搜索历史")
    @GetMapping("/history")
    public Result<List<SearchHistory>> getUserSearchHistory(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") int limit,
            Authentication authentication) {
        
        Long userId = Long.parseLong(authentication.getName());
        List<SearchHistory> history = searchService.getUserSearchHistory(userId, limit);
        return Result.success(history);
    }
    
    /**
     * 清除搜索历史
     */
    @Operation(summary = "清除搜索历史")
    @DeleteMapping("/history")
    public Result<Void> clearSearchHistory(Authentication authentication) {
        Long userId = Long.parseLong(authentication.getName());
        searchService.clearUserSearchHistory(userId);
        return Result.success();
    }
}
